<?php
/*
  $Id:$
*/

  define('TITLE', '页面管理');
  define('TABLE', 'page');
  define('MAX_DISPLAY_SEARCH_RESULTS', 20);

  require('include/application_top.php');

  $action = (isset($_GET['action']) ? $_GET['action'] : '');

  if (!is_null($action)) {
    switch ($action) {
      case 'set_status':
        db_update(TABLE,array(
          'status' => $_GET['status']
        ),
        '`id` = ' . intval($_GET['id']));
        redirect('?page=' . $_GET['page'] . '&id=' . intval($_GET['id']));
        break;
      case 'insert':
        db_insert(TABLE, array(
          'id'         => 'null', 
          'parent_id'  => intval($_POST['parent_id']), 
          'title'      => $_POST['title'], 
          'content'    => $_POST['content'], 
          'viewed'     => 0, 
          'status'     => $_POST['status'], 
          'sort_order' => $_POST['sort_order'], 
          'created_at' => 'now()', 
          'updated_at' => 'now()'
        ));
        $messageStack->add_session('页面创建成功');
        redirect('?page=' . $_POST['page'] . '&id=' . db_insert_id());
        break;
      case 'save':
        db_update(TABLE, array(
          'parent_id'  => intval($_POST['parent_id']),
          'title'      => $_POST['title'], 
          'content'    => $_POST['content'], 
          'status'     => $_POST['status'], 
          'sort_order' => $_POST['sort_order'], 
          'updated_at' => 'now()'
          ), 
          '`id` = ' . intval($_POST['id'])
        );
        $messageStack->add_session('页面更新成功');
        redirect('?page=' . $_POST['page'] . '&id=' . intval($_POST['id']));
        break;
      case 'delete':
        db_query("delete from ".TABLE." where id = '" . intval($_GET['id']) . "'");
        $messageStack->add_session('页面删除成功');
        redirect('?page=' . $_GET['page']);
        break;
    }
  }
?>
<?php include 'include/header.php';?>
<?php include 'include/column_left.php';?>
    <div id="right">
      <table id="module_information" border="0" width="100%" cellspacing="0" cellpadding="0">
        <tr>
          <td><h2><?php echo TITLE;?></h2></td>
        </tr>
      </table>
      <table id="module_table" border="0" width="100%" cellspacing="0" cellpadding="0">
        <tr>
          <td valign="top">
            <!-- listing -->
            <table id="module_listing" border="0" width="100%" cellspacing="0" cellpadding="0">
              <tr class="table_heading_row">
                <th align="left" class="table_heading left_th">标题</th>
                <th align="left" class="table_heading right_th">状态</th>
              </tr>
<?php
  $query_numrows = 0;
  !isset($_GET['page']) && $_GET['page'] = 1;

  $query_raw = "select * from ".TABLE." order by sort_order desc";
  $listing_split = new splitPageResults($_GET['page'], MAX_DISPLAY_SEARCH_RESULTS, $query_raw, $query_numrows);
  
  foreach (db_fetch_all($query_raw) as $item) {
    if ((!isset($_GET['id']) && !isset($info)) || (isset($_GET['id']) && $_GET['id'] == $item['id'])) $info = $item;
?>
              <tr onclick="document.location.href='?page=<?php echo $_GET['page'];?>&id=<?php echo $item['id'];?>'"
                  onmouseover="this.className='table_row_mouse_over'"
<?php if (isset($info['id']) && $item['id'] == $info['id']) {?>
                  class="select_row"
                  onmouseout="this.className='select_row'"
<?php } else { ?>
                  onmouseout="this.className='table_row_mouse_out'"
<?php } ?>
              >
                <td><?php echo $item['title'];?></td>
                <td>
                  <?php if ($item['status'] == 'visiable') {?>
                    <a href="?action=set_status&page=<?php echo $_GET['page'];?>&id=<?php echo $item['id'];?>&status=disabled">公开</a>
                  <?php } else {?>
                    <a href="?action=set_status&page=<?php echo $_GET['page'];?>&id=<?php echo $item['id'];?>&status=visiable">草稿</a>
                  <?php }?>
                </td>
              </tr>
<?php }?>
              <tr>
                <td colspan="2">
                  <table id='listing_actions' border="0" width="100%" cellspacing="0" cellpadding="0">
                    <!-- page links -->
                    <tr>
                      <td align="left"><?php echo $listing_split->display_count($query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $_GET['page'], '显示 <b>%d</b> 到 <b>%d</b> (共 <b>%d</b> 个页面)'); ?></td>
                      <td align="right"><?php echo $listing_split->display_links($query_numrows, MAX_DISPLAY_SEARCH_RESULTS, 5, $_GET['page']); ?></td>
                    </tr>
<?php if (empty($action)) {?>
                    <!-- actions -->
                    <tr>
                      <td colspan="2" align="right"><a href="?action=new&page=<?php echo $_GET['page'];?>">新建页面</a></td>
                    </tr>
<?php }?>
                  </table>
                </td>
              </tr>
            </table>
          </td>
<?php if ($query_numrows) { ?>
          <!-- information -->
          <td id="item_information" class="information" width="30%" valign="top">
<?php
  switch ($action) {
    case 'new':
?>
<h3>新建页面</h3>
<form action="?action=insert" method="post">
<input type="hidden" name="page" value="<?php echo $_GET['page'];?>" />
<input type="hidden" name="parent_id" value="0" />
<fieldset><legend>标题</legend><input type="text" name="title" /></fieldset>
<fieldset><legend>内容</legend><textarea name="content"></textarea></fieldset>
<fieldset><legend>状态</legend>
  <select name="status">
    <option value="visiable">公开</option>
    <option value="disabled">草稿</option>
  </select>
</fieldset>
<fieldset><legend>排序</legend><input type="text" name="sort_order" value="0" /></fieldset>
<p><input type="submit" /> <input type="reset" /></p>
</form>
<?php
      break;
    case 'edit':
?>
<h3>编辑页面</h3>
<form action="?action=save" method="post">
<input type="hidden" name="page" value="<?php echo $_GET['page'];?>" />
<input type="hidden" name="id" value="<?php echo $info['id'];?>" />
<input type="hidden" name="parent_id" value="0" />
<fieldset><legend>标题</legend><input type="text" name="title" value="<?php echo $info['title'];?>" /></fieldset>
<fieldset><legend>内容</legend><textarea name="content"><?php echo $info['content'];?></textarea></fieldset>
<fieldset><legend>状态</legend>
  <select name="status">
    <option value="visiable"<?php if($info['status'] == 'visiable'){?> selected="selected"<?php }?>>公开</option>
    <option value="disabled"<?php if($info['status'] == 'disabled'){?> selected="selected"<?php }?>>草稿</option>
  </select>
</fieldset>
<fieldset><legend>排序</legend><input type="text" name="sort_order" value="<?php echo $info['sort_order'];?>" /></fieldset>
<p><input type="submit" /> <input type="reset" /></p>
</form>
<?php
      break;
    case 'delete_confirm':
?>
<h3>删除页面</h3>
<p>删除将不可恢复, 确认删除么?</p>
<p>
  <input value="确认" type="submit" onclick='document.location.href="?action=delete&page=<?php echo $_GET['page'];?>&&id=<?php echo $info['id'];?>"' />
  <input value="取消" type="submit" onclick='document.location.href="?page=<?php echo $_GET['page'];?>&id=<?php echo $info['id'];?>"' />
</p>
<?php
      break;
    default:
?>
<h3>页面信息</h3>
<div class="object_action">
<a href="?action=edit&page=<?php echo $_GET['page'];?>&id=<?php echo $info['id'];?>">编辑</a>
<a href="?action=delete_confirm&page=<?php echo $_GET['page'];?>&&id=<?php echo $info['id'];?>">删除</a>
</div>
<h4><?php echo $info['title'];?></h4>
<?php echo $info['content'];?>
<?php
      break;
  }
?>
          </td>
<?php }?>
        </tr>
      </table>
    </div>
<?php include 'include/footer.php';?>
<?php require('include/application_bottom.php');?>